/**
 * 封装http请求
 * get
 * post 
 * put
 * 增加header
 * 2019年7月30日 13:58:52
 */
 import { Injectable } from '@angular/core';
 import { HttpClient, HttpHeaders } from '@angular/common/http';
 import { BaseService } from './base.service';
 import { environment } from '../../environments/environment';
 import { timeout } from 'rxjs/operators';
 
 const httpOptions = {
   headers: new HttpHeaders({
     'Content-Type': 'application/json',
     'signature': "",
     'os': "3",
     'imei': "",
     'timestamp': "",
     'Authorization': '',
     'osVersion': '',
     'Cache-Control':'no-cache'
   })
 };
 export interface Config {
   code: any;
   data: any;
 }
 @Injectable()
 export class HttpPcSerice {
   baseUrl: string = environment.baseUrlPc;
   constructor(private http: HttpClient, private userservice: BaseService) {
 
   }
   httpGet(url: string, suc: any, err: any) {
     let timestamp = (new Date()).getTime();
     let imei = this.userservice.getImei();
     let signature = this.userservice.getMd5NcodePC(timestamp);
     let token = this.userservice.getUserToken();
     let osVersion = this.userservice.getBrowserVersion();
     httpOptions.headers = httpOptions.headers.set('imei', imei);
     httpOptions.headers = httpOptions.headers.set('timestamp', timestamp + "");
     httpOptions.headers = httpOptions.headers.set('signature', signature);
     httpOptions.headers = httpOptions.headers.set('Authorization', token);
     httpOptions.headers = httpOptions.headers.set("osVersion", osVersion);
     this.http.get<Config>(url, httpOptions).pipe(timeout(80000)).subscribe((res: Config) => {  
         suc(res);  
     }, error => {
       err(error);
       if (navigator.onLine) { //系统出错
        /*  this.userservice.errorSub.next(2);
         this.userservice.toastSub.next({ type: 1, message: "小主，系统出错了~" }); */
       }
       else { //网络差
        /*  this.userservice.errorSub.next(1);
         this.userservice.toastSub.next({ type: 1, message: "小主，网络开小差了~" }); */
       }
     })
   }
   httpPost(url: string, data: any, suc: any,err:any) {
     let timestamp = (new Date()).getTime();
     let imei = this.userservice.getImei();
     let signature = this.userservice.getMd5NcodePC(timestamp);
     let token = this.userservice.getUserToken();
     let osVersion = this.userservice.getBrowserVersion();
     httpOptions.headers = httpOptions.headers.set('imei', imei);
     httpOptions.headers = httpOptions.headers.set('timestamp', timestamp + "");
     httpOptions.headers = httpOptions.headers.set('signature', signature);
     httpOptions.headers = httpOptions.headers.set('Authorization', token);
     httpOptions.headers = httpOptions.headers.set("osVersion", osVersion);
     return this.http.post<Config>(url, data, httpOptions).pipe(timeout(80000)).subscribe((res: Config) => {    
         suc(res);
     },error=>{
       err(error);
       if (navigator.onLine) { //系统出错
         /* this.userservice.errorSub.next(2);
         this.userservice.toastSub.next({ type: 1, message: "小主，系统出错了~" }); */
       }
       else { //网络差
       /*   this.userservice.errorSub.next(1);
         this.userservice.toastSub.next({ type: 1, message: "小主，网络开小差了~" }); */
       }
     })
   }
   httpGetNoPara(url: string, suc: any, err: any) {
     this.http.get(url, { responseType: 'text' }).subscribe(res => {
       suc(res);
     }, error => {
       err(error);
     })
   }
   httpPut(url: string, data: any, suc: any) {
     let timestamp = (new Date()).getTime();
     let imei = this.userservice.getImei();
     let signature = this.userservice.getMd5NcodePC(timestamp);
     let token = this.userservice.getUserToken();
     let osVersion = this.userservice.getBrowserVersion();
 
     httpOptions.headers = httpOptions.headers.set('imei', imei);
     httpOptions.headers = httpOptions.headers.set('reqtime', timestamp + "");
     httpOptions.headers = httpOptions.headers.set('signature', signature);
     httpOptions.headers = httpOptions.headers.set('Authorization', token);
     httpOptions.headers = httpOptions.headers.set("osVersion", osVersion);
     return this.http.put<Config>(url, data, httpOptions).subscribe((res: Config) => {
       suc(res);
     })
   }
 }